/**
 * @program: pettyloan
 * @ClassName RepayRecordController
 * @author: 汪江
 * @create: 2022-12-07 14:06
 **/
package com.wn.pettyloan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.pettyloan.dto.PayDto;
import com.wn.pettyloan.dto.RepayRecordDto;
import com.wn.pettyloan.entity.PayType;
import com.wn.pettyloan.entity.Repay;
import com.wn.pettyloan.entity.RepayRecord;
import com.wn.pettyloan.service.PayTypeService;
import com.wn.pettyloan.service.RepayRecordService;
import com.wn.pettyloan.service.RepayService;
import com.wn.pettyloan.util.Result;
import com.wn.pettyloan.vo.RepayRecordVO;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Component
@RestController
@RequestMapping("repayRecord")
public class RepayRecordController {
    @Resource
    private RepayRecordService repayRecordService;
    @Resource
    private RepayService repayService;
    @Resource
    private PayTypeService payTypeService;

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody RepayRecordDto repayRecordDto) {
        /** @Author 汪江
         * @Description queryAll 返回该合同每期还款信息
         * @Date 15:10 2022/12/7
         * @Param [repayRecordDto]
         * @return com.wn.pettyloan.util.Result
         **/
        PageHelper.startPage(repayRecordDto.getCurrentPage(), repayRecordDto.getPageSize(), true);
        List<RepayRecordVO> repayRecordVOS = repayRecordService.selRepayRecordById(repayRecordDto);
        PageInfo<RepayRecordVO> repayRecordVOPageInfo = new PageInfo<>(repayRecordVOS);
        RepayRecordVO repayRecordVO = repayRecordService.selContractByid(repayRecordDto);
        if (repayRecordVOS != null) {
            if (repayRecordVO != null) {
                return Result.ok().data("RepayInfo", repayRecordVO).data("list", repayRecordVOS).data("total", repayRecordVOPageInfo.getTotal());
            } else {
                return Result.error();
            }
        } else {
            return Result.error();
        }
    }
    @PostMapping("repayInfo")
    public Result repayInfo(@RequestBody PayDto payDto){
        long repayReocrdID = payDto.getRepayReocrdId();//获取本次订单的id
        RepayRecord repayRecord = repayRecordService.selectByPrimaryKey(repayReocrdID);//查本次的还款记录
        if (repayRecord != null) {
            List<PayType> payTypes = payTypeService.selAllPayType();
            if (payTypes != null) {
                return Result.ok().data("repayRecord",repayRecord).data("payTypeList",payTypes);
            }
        }
        return Result.error();
    }
    @PostMapping("paySuccess")
    public Result paySuccess(@RequestBody PayDto payDto) {
        boolean b = repayRecordService.paySuccess(payDto);
        if(b){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    @PostMapping("manageRepay")
    public Result companyRepayRecord(@RequestBody RepayRecordDto repayRecordDto){
        PageHelper.startPage(repayRecordDto.getCurrentPage(), repayRecordDto.getPageSize(), true);
        List<RepayRecordVO> repayRecordVOS = repayRecordService.companyRepayRecord(repayRecordDto);
        PageInfo<RepayRecordVO> repayRecordVOPageInfo = new PageInfo<>(repayRecordVOS);
        if (repayRecordVOS != null) {
            return Result.ok().data("list",repayRecordVOS).data("total",repayRecordVOPageInfo.getTotal());
        }else {
            return Result.error();
        }
    }
}
